<template>
  <div class="agri-bg">
    <div class="agri-header">
      <svg width="60" height="60" viewBox="0 0 60 60" fill="none" xmlns="http://www.w3.org/2000/svg">
        <ellipse cx="30" cy="30" rx="28" ry="18" fill="#e8f5e9"/>
        <path d="M15 40 Q30 10 45 40" stroke="#67c23a" stroke-width="3" fill="none"/>
        <ellipse cx="30" cy="30" rx="6" ry="12" fill="#a5d6a7"/>
      </svg>
      <h2 class="login-title">
        欢迎回来
        <svg class="leaf-icon" width="22" height="22" viewBox="0 0 22 22" fill="none" xmlns="http://www.w3.org/2000/svg">
          <path d="M2 20 Q12 2 20 10 Q10 18 2 20 Z" fill="#67c23a"/>
        </svg>
      </h2>
      <p class="login-subtitle">请登录您的账号</p>
    </div>
    <div class="login-container">
      <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form">
        <div class="form-content">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="el-icon-user">
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="密码" prefix-icon="el-icon-lock">
            </el-input>
          </el-form-item>

          <el-form-item>
            <div class="validate-container">
              <el-input v-model="loginForm.validCode" placeholder="验证码" prefix-icon="el-icon-key">
              </el-input>
              <ValidCode @input="createValidCode" class="validate-code" />
            </div>
          </el-form-item>

          <el-form-item>
            <el-button type="success" class="login-button" @click="onLogin">
              登录
            </el-button>
          </el-form-item>

          <div class="login-actions">
            <el-link type="success" @click="$router.push('/forget')" class="forget-link">
              <i class="el-icon-question"></i> 忘记密码？
            </el-link>
            <div class="register-link">
              还没有账号？
              <el-link type="success" @click="toRegister">立即注册</el-link>
            </div>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import ValidCode from "../components/Validate";
import request from "@/utils/request";
import { setRoutes } from "@/router";



export default {
  name: 'Login',
  components: {
    ValidCode
  },
  data() {
    return {
      validCode: '', //通过valicode获取的验证码
      loginForm: {
        username: '',
        password: '',
        validCode: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    toRegister() {
      this.$router.push("/register");
    },
    createValidCode(data) {
      this.validCode = data
    },
    onLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 验证码比较时转换为小写，实现不区分大小写
          if (this.loginForm.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
            this.$message.error("验证码错误");
            return;
          }
          request.post("/user/login", this.loginForm)
            .then(res => {
              if (res.code === "0") {
                this.$message.success("登录成功");
                if (res.data.token) {
                  localStorage.setItem("token", res.data.token);
                }
                if (res.data) {
                  if (res.data.role === 'USER') {
                    localStorage.setItem("frontUser", JSON.stringify(res.data));
                  } else {
                    localStorage.setItem("backUser", JSON.stringify(res.data));
                  }
                }

                // 根据用户角色决定跳转路径
                if (res.data.role !== 'USER') {
                  if (res.data.menuList) {
                    localStorage.setItem("userMenuList", JSON.stringify(res.data.menuList));
                  }
                  setRoutes();
                  this.$router.push('/showView');
                } else {
          
                  this.$router.push('/');
                }
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              console.error("登录失败:", error);
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.agri-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #e8f5e9 0%, #fffde7 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.agri-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 18px;
}

.login-title {
  font-size: 32px;
  color: #388e3c;
  margin-bottom: 8px;
  text-align: center;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.leaf-icon {
  vertical-align: middle;
}

.login-subtitle {
  font-size: 16px;
  color: #8d6e63;
  margin-bottom: 32px;
  text-align: center;
}

.login-container {
  width: 100%;
  max-width: 400px;
  background: #f9fbe7;
  border-radius: 22px;
  box-shadow: 0 6px 32px 0 rgba(103, 194, 58, 0.10), 0 1.5px 6px 0 #a1887f22;
  padding: 36px 32px 28px 32px;
  margin: 0 auto;
  animation: fadeIn 0.6s ease-out;
}

.login-form {
  margin-top: 0;
}

.form-content {
  width: 90%;
  margin: 0 auto;
}

.validate-container {
  display: flex;
  gap: 16px;
}

.validate-code {
  flex-shrink: 0;
  width: 120px;
  border-radius: 8px;
  overflow: hidden;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
  margin-top: 20px;
  border-radius: 10px;
  background: linear-gradient(90deg, #67c23a 0%, #43a047 100%);
  color: #fff;
  border: none;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.login-button:hover {
  background: linear-gradient(90deg, #43a047 0%, #67c23a 100%);
  transform: translateY(-2px) scale(1.03);
  box-shadow: 0 4px 16px rgba(103, 194, 58, 0.18);
}

.login-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 24px;
  font-size: 14px;
}

.forget-link {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #43a047;
}

.register-link {
  color: #8d6e63;
}

:deep(.el-input__inner) {
  height: 44px;
  line-height: 44px;
  border-radius: 10px;
  border: 1.5px solid #a5d6a7;
  background: #fff;
  transition: all 0.3s ease;
  font-size: 15px;
}

:deep(.el-input__inner:focus) {
  border-color: #67c23a;
  box-shadow: 0 0 0 2px rgba(103, 194, 58, 0.13);
}

:deep(.el-form-item) {
  margin-bottom: 22px;
}

:deep(.el-input__prefix) {
  left: 12px;
  top: 0;
  height: 100%;
  display: flex;
  align-items: center;
}

:deep(.el-input__prefix i) {
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
}

:deep(.el-input__inner) {
  padding-left: 38px;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 480px) {
  .form-content {
    width: 100%;
  }
  .login-container {
    padding: 18px 6px 18px 6px;
  }
}
</style>